<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Pandas分组聚合2 | Python 数据分析学习目录</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../数据分析库的操作/10Pandas数据规整-清理.html" />
    
    
    <link rel="prev" href="../数据分析库的操作/8Pandas分组聚合1.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4.4.5"
        data-chapter-title="Pandas分组聚合2"
        data-filepath="数据分析库的操作/9Pandas分组聚合2.md"
        data-basepath=".."
        data-revision="Wed Oct 24 2018 21:30:49 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        数据分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="Python数据分析序言/内容序言.html">
            
                
                    <a href="../Python数据分析序言/内容序言.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python数据分析内容
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="python数据分析环境和工具/Python数据分析相关.html">
            
                
                    <a href="../python数据分析环境和工具/Python数据分析相关.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        python数据分析环境和工具
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="python数据分析环境和工具/1Python数据课程软件和环境安装.html">
            
                
                    <a href="../python数据分析环境和工具/1Python数据课程软件和环境安装.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Python数据课程 软件和环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="python数据分析环境和工具/2python发行版.html">
            
                
                    <a href="../python数据分析环境和工具/2python发行版.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        python发行版
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="python数据分析环境和工具/5交互式编辑器-JupyterNotebook.html">
            
                
                    <a href="../python数据分析环境和工具/5交互式编辑器-JupyterNotebook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        交互式编辑器-JupyterNotebook
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="python数据分析环境和工具/5.1Jupyter-notebook拓展应用.html">
            
                
                    <a href="../python数据分析环境和工具/5.1Jupyter-notebook拓展应用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.</b>
                        
                        Jupyter-notebook拓展应用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="python数据分析环境和工具/包和环境管理器：conda.html">
            
                
                    <a href="../python数据分析环境和工具/包和环境管理器：conda.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        包和环境管理器：conda
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="python数据分析环境和工具/pip和Virtualenv.html">
            
                
                    <a href="../python数据分析环境和工具/pip和Virtualenv.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.1.</b>
                        
                        pip和Virtualenv
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="python数据分析环境和工具/3Markdown.html">
            
                
                    <a href="../python数据分析环境和工具/3Markdown.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        标记语言：Markdown
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="python数据分析环境和工具/4Markdown语法.html">
            
                
                    <a href="../python数据分析环境和工具/4Markdown语法.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.1.</b>
                        
                        Markdown语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="python数据分析环境和工具/6Gitbook文档.html">
            
                
                    <a href="../python数据分析环境和工具/6Gitbook文档.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.2.</b>
                        
                        文档管理工具-Gitbook
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="数据分析库的初步认识/index.html">
            
                
                    <a href="../数据分析库的初步认识/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据分析库-Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="数据分析库的初步认识/Pandas创建.html">
            
                
                    <a href="../数据分析库的初步认识/Pandas创建.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        pandas
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="数据分析库的初步认识/Series创建.html">
            
                
                    <a href="../数据分析库的初步认识/Series创建.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Series
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="数据分析库的初步认识/DataFrame创建.html">
            
                
                    <a href="../数据分析库的初步认识/DataFrame创建.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        DataFrame对象-创建
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" >
            
            <span><b>4.</b> 数据分析库的操作</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="数据分析库的操作/1DataFrame查询1-整体.html">
            
                
                    <a href="../数据分析库的操作/1DataFrame查询1-整体.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        DataFrame查询1-整体
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="数据分析库的操作/2DataFrame查询2-专用查询.html">
            
                
                    <a href="../数据分析库的操作/2DataFrame查询2-专用查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        DataFrame查询2-专用查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="数据分析库的操作/3DataFrame查询3-专有查询：过滤查询.html">
            
                
                    <a href="../数据分析库的操作/3DataFrame查询3-专有查询：过滤查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        DataFrame查询3-专有查询：过滤查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="数据分析库的操作/4Pandas对象的数据操作：增删改查.html">
            
                
                    <a href="../数据分析库的操作/4Pandas对象的数据操作：增删改查.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Pandas对象的数据操作：增删改查
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="数据分析库的操作/5Pandas数据操作：其他操作.html">
            
                
                    <a href="../数据分析库的操作/5Pandas数据操作：其他操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        Pandas数据操作：其他操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="数据分析库的操作/6Pandas数据存取.html">
            
                
                    <a href="../数据分析库的操作/6Pandas数据存取.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        Pandas数据存取
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="数据分析库的操作/7Pandas数据运算.html">
            
                
                    <a href="../数据分析库的操作/7Pandas数据运算.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        Pandas数据运算
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.3.1" data-path="数据分析库的操作/7.1Pandas数据运算拓展.html">
            
                
                    <a href="../数据分析库的操作/7.1Pandas数据运算拓展.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.1.</b>
                        
                        Pandas数据运算-拓展
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="数据分析库的操作/8Pandas分组聚合1.html">
            
                
                    <a href="../数据分析库的操作/8Pandas分组聚合1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        Pandas分组聚合1
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.4.5" data-path="数据分析库的操作/9Pandas分组聚合2.html">
            
                
                    <a href="../数据分析库的操作/9Pandas分组聚合2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.5.</b>
                        
                        Pandas分组聚合2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.6" data-path="数据分析库的操作/10Pandas数据规整-清理.html">
            
                
                    <a href="../数据分析库的操作/10Pandas数据规整-清理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.6.</b>
                        
                        Pandas数据规整-清理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.7" data-path="数据分析库的操作/11Pandas数据规整-转换.html">
            
                
                    <a href="../数据分析库的操作/11Pandas数据规整-转换.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.7.</b>
                        
                        Pandas数据规整-转换
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.7.1" data-path="数据分析库的操作/14Pandas数据规整-转换-离散化和面元划分.html">
            
                
                    <a href="../数据分析库的操作/14Pandas数据规整-转换-离散化和面元划分.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.7.1.</b>
                        
                        离散化和面元划分
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.4.8" data-path="数据分析库的操作/16Pandas数据规整-合并.html">
            
                
                    <a href="../数据分析库的操作/16Pandas数据规整-合并.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.8.</b>
                        
                        Pandas数据规整-合并
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="数据分析库的操作/13Pandas数据规整-重塑和轴向旋转.html">
            
                
                    <a href="../数据分析库的操作/13Pandas数据规整-重塑和轴向旋转.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        Pandas数据规整-重塑和轴向旋转
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.5.1" data-path="数据分析库的操作/13.1透视表和交叉表.html">
            
                
                    <a href="../数据分析库的操作/13.1透视表和交叉表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.1.</b>
                        
                        透视表和交叉表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="Python可视化/绘图库-Matplotlib.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        Python可视化
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="Python可视化/绘图库-Matplotlib/Matplotlib常见图表.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/Matplotlib常见图表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        基础：Matplotlib常见图表
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="Python可视化/绘图库-Matplotlib/Matplotlib常见设置和操作.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/Matplotlib常见设置和操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.1.</b>
                        
                        Matplotlib常见设置和操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="Python可视化/绘图库-Matplotlib/1Matplotlib-绘图区域.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/1Matplotlib-绘图区域.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        提升：绘图区域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="Python可视化/绘图库-Matplotlib/2Matplotlib-图像组件.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/2Matplotlib-图像组件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        提升：绘图组件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="Python可视化/绘图库-Matplotlib/3Matplotlib-高级绘图.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/3Matplotlib-高级绘图.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        拓展：高级绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="Python可视化/绘图库-Matplotlib/4数学计算展示图像.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/4数学计算展示图像.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        拓展：数学计算展示图像
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="Python可视化/绘图库-Matplotlib/5注意事项.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/5注意事项.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        拓展：注意事项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="Python可视化/绘图库-Matplotlib/6pylab.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/6pylab.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        拓展：pylab
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="数据分析必备知识点/数据分析必备知识点汇集.html">
            
                
                    <a href="../数据分析必备知识点/数据分析必备知识点汇集.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据分析必备知识点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="数据分析必备知识点/数据分析流程.html">
            
                
                    <a href="../数据分析必备知识点/数据分析流程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        数据分析流程
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python 数据分析学习目录</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h3 id="&#x6839;&#x636E;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#x7684;&#x7EA7;&#x522B;&#x5206;&#x7EC4;&#xB6;">&#x6839;&#x636E;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#x7684;&#x7EA7;&#x522B;&#x5206;&#x7EC4;&#xB6;</h3>
<p>&#x8981;&#x6839;&#x636E;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#x7684;&#x7EA7;&#x522B;&#x5206;&#x7EC4;&#xFF0C;&#x4F7F;&#x7528;level&#x5173;&#x952E;&#x5B57;&#x4F20;&#x9012;&#x7EA7;&#x522B;&#x5E8F;&#x53F7;&#x6216;&#x540D;&#x5B57;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
</code></pre>
<pre><code class="lang-python">columns = pd.MultiIndex.from_arrays([[<span class="hljs-string">&apos;US&apos;</span>, <span class="hljs-string">&apos;US&apos;</span>, <span class="hljs-string">&apos;US&apos;</span>, <span class="hljs-string">&apos;JP&apos;</span>, <span class="hljs-string">&apos;JP&apos;</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>]],  names=[<span class="hljs-string">&apos;cty&apos;</span>, <span class="hljs-string">&apos;tenor&apos;</span>])
hier_df = pd.DataFrame(np.random.randn(<span class="hljs-number">4</span>, <span class="hljs-number">5</span>), columns=columns)
hier_df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th>cty</th>
      <th colspan="3" halign="left">US</th>
      <th colspan="2" halign="left">JP</th>
    </tr>
    <tr>
      <th>tenor</th>
      <th>1</th>
      <th>3</th>
      <th>5</th>
      <th>1</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-1.092580</td>
      <td>1.353454</td>
      <td>0.659464</td>
      <td>-0.146829</td>
      <td>-0.488205</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.361819</td>
      <td>-1.735350</td>
      <td>-0.027743</td>
      <td>1.138146</td>
      <td>-0.130812</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.270425</td>
      <td>0.524885</td>
      <td>-0.238268</td>
      <td>0.008854</td>
      <td>1.397235</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.238916</td>
      <td>-0.441246</td>
      <td>0.609524</td>
      <td>-0.644583</td>
      <td>1.427737</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">hier_df.index
hier_df.columns
</code></pre>
<pre><code>MultiIndex(levels=[[&apos;JP&apos;, &apos;US&apos;], [1, 3, 5]],
           labels=[[1, 1, 1, 0, 0], [0, 1, 2, 0, 1]],
           names=[&apos;cty&apos;, &apos;tenor&apos;])
</code></pre><pre><code class="lang-python">hier_df.groupby(level=<span class="hljs-string">&apos;cty&apos;</span>, axis=<span class="hljs-number">1</span>).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>cty</th>
      <th>JP</th>
      <th>US</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-0.635034</td>
      <td>0.920338</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.007334</td>
      <td>-1.401274</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1.406088</td>
      <td>0.557042</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0.783153</td>
      <td>-0.070638</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">-<span class="hljs-number">0.146829</span> + -<span class="hljs-number">0.488205</span>
</code></pre>
<pre><code>-0.635034
</code></pre><pre><code class="lang-python">hier_df.groupby(level=<span class="hljs-string">&apos;tenor&apos;</span>, axis=<span class="hljs-number">1</span>).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>tenor</th>
      <th>1</th>
      <th>3</th>
      <th>5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-1.239409</td>
      <td>0.865249</td>
      <td>0.659464</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.499966</td>
      <td>-1.866162</td>
      <td>-0.027743</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.279278</td>
      <td>1.922120</td>
      <td>-0.238268</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.883499</td>
      <td>0.986491</td>
      <td>0.609524</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">-<span class="hljs-number">1.092580</span> + -<span class="hljs-number">0.146829</span>
</code></pre>
<pre><code>-1.2394090000000002
</code></pre><h3 id="&#x6839;&#x636E;&#x5217;&#x8868;&#x5206;&#x7EC4;">&#x6839;&#x636E;&#x5217;&#x8868;&#x5206;&#x7EC4;</h3>
<pre><code class="lang-python">df = pd.DataFrame({
    <span class="hljs-string">&apos;name&apos;</span>: [<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>,<span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>,<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>,<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-string">&apos;&#x8D75;&#x516D;&apos;</span>],
    <span class="hljs-string">&apos;chinese&apos;</span>:np.random.randint(<span class="hljs-number">35</span>,<span class="hljs-number">100</span>,<span class="hljs-number">7</span>),
    <span class="hljs-string">&apos;math&apos;</span>:np.random.randint(<span class="hljs-number">35</span>,<span class="hljs-number">100</span>,<span class="hljs-number">7</span>),
    <span class="hljs-string">&apos;english&apos;</span>:np.random.randint(<span class="hljs-number">35</span>,<span class="hljs-number">100</span>,<span class="hljs-number">7</span>),
    <span class="hljs-string">&apos;test&apos;</span>: [<span class="hljs-string">&apos;&#x4E00;&apos;</span>,<span class="hljs-string">&apos;&#x4E00;&apos;</span>,<span class="hljs-string">&apos;&#x4E00;&apos;</span>,<span class="hljs-string">&apos;&#x4E8C;&apos;</span>,<span class="hljs-string">&apos;&#x4E8C;&apos;</span>,<span class="hljs-string">&apos;&#x4E09;&apos;</span>,<span class="hljs-string">&apos;&#x4E00;&apos;</span>]
})

df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df[<span class="hljs-string">&apos;name&apos;</span>]
</code></pre>
<pre><code>0    &#x5F20;&#x4E09;
1    &#x674E;&#x56DB;
2    &#x738B;&#x4E94;
3    &#x674E;&#x56DB;
4    &#x738B;&#x4E94;
5    &#x738B;&#x4E94;
6    &#x8D75;&#x516D;
Name: name, dtype: object
</code></pre><pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>254</td>
      <td>182</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(df[<span class="hljs-string">&apos;name&apos;</span>]).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>254</td>
      <td>182</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">xxx = [<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>,<span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>,<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>,<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>]  <span class="hljs-comment"># &#x8D75;&#x516D;&#x53D8;&#x6210;&#x5F20;&#x4E09;</span>
df.groupby(xxx).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>129</td>
      <td>135</td>
      <td>167</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>254</td>
      <td>182</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-number">47</span> + <span class="hljs-number">82</span>
</code></pre>
<pre><code>129
</code></pre><p>&#x6839;&#x636E;&#x5B57;&#x5178;&#x5BF9;&#x8C61;&#x5206;&#x7EC4;</p>
<pre><code class="lang-python">df2 = df.set_index(<span class="hljs-string">&apos;name&apos;</span>)
df2
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x884C;&#x7D22;&#x5F15;&#xFF0C;&#x5F20;&#x4E09;&#x3001;&#x8D75;&#x516D;&#x5408;&#x5E76;&#x4E3A;&#x201C;&#x4E00;&#x4E2A;&#x4EBA;&#x201D;&#xFF0C;&#x674E;&#x56DB;&#x6539;&#x4E3A;&#x2018;&#x674E;&#x56DB;new&#x2019;,&#x738B;&#x4E94;&#x4E0D;&#x5728;&#x5B57;&#x5178;&#x4E2D;&#xFF0C;&#x8FC7;&#x6EE4;&#x6389;</span>
mapping2 = {<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>: <span class="hljs-string">&apos;&#x4E00;&#x4E2A;&#x4EBA;&apos;</span>, <span class="hljs-string">&apos;&#x8D75;&#x516D;&apos;</span>: <span class="hljs-string">&apos;&#x4E00;&#x4E2A;&#x4EBA;&apos;</span>, <span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>: <span class="hljs-string">&apos;&#x674E;&#x56DB;new&apos;</span>}
df2.groupby(mapping2).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x4E00;&#x4E2A;&#x4EBA;</th>
      <td>129</td>
      <td>135</td>
      <td>167</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;new</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df2.groupby([<span class="hljs-string">&apos;name&apos;</span>, mapping2]).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <th>&#x4E00;&#x4E2A;&#x4EBA;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <th>&#x674E;&#x56DB;new</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <th>&#x4E00;&#x4E2A;&#x4EBA;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>



<hr>
<h2 id="&#x7EC3;&#x4E60;&#xFF1A;-&#x8F93;&#x51FA;&#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#x5728;&#x6BCF;&#x6B21;&#x8003;&#x8BD5;&#x6B21;&#x6570;&#x4E2D;&#x7684;&#x6570;&#x5B57;&#x5E73;&#x5747;&#x5206;">&#x7EC3;&#x4E60;&#xFF1A; &#x8F93;&#x51FA;&#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#x5728;&#x6BCF;&#x6B21;&#x8003;&#x8BD5;&#x6B21;&#x6570;&#x4E2D;&#x7684;&#x6570;&#x5B57;&#x5E73;&#x5747;&#x5206;</h2>
<p>&#x4ECE;&#x4E00;&#x4E2A;&#x539F;&#x59CB;&#x5927;&#x8868;&#x4E2D;&#x62BD;&#x53D6;&#x4E00;&#x4E2A;&#x7B26;&#x5408;&#x9700;&#x6C42;&#x7684;&#x5C0F;&#x8868;</p>
<pre><code class="lang-python">df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#xFF0C;&#x6240;&#x6709;&#x8003;&#x8BD5;&#xFF0C;&#x6240;&#x6709;&#x6210;&#x7EE9;&#x5E73;&#x5747;&#x5206;&#x3002;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).mean()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>72.500000</td>
      <td>77.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>80.666667</td>
      <td>84.666667</td>
      <td>60.666667</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#xFF0C;&#x6240;&#x6709;&#x8003;&#x8BD5;&#xFF0C;&#x6570;&#x5B66;&#x6210;&#x7EE9;&#x5E73;&#x5747;&#x5206;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>)[<span class="hljs-string">&apos;math&apos;</span>].mean()
</code></pre>
<pre><code>name
&#x5F20;&#x4E09;    89.000000
&#x674E;&#x56DB;    77.000000
&#x738B;&#x4E94;    84.666667
&#x8D75;&#x516D;    46.000000
Name: math, dtype: float64
</code></pre><pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).mean()[<span class="hljs-string">&apos;math&apos;</span>]
</code></pre>
<pre><code>name
&#x5F20;&#x4E09;    89.000000
&#x674E;&#x56DB;    77.000000
&#x738B;&#x4E94;    84.666667
&#x8D75;&#x516D;    46.000000
Name: math, dtype: float64
</code></pre><pre><code class="lang-python">df.groupby([<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;test&apos;</span>]).mean()  <span class="hljs-comment"># &#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#xFF0C;&#x6BCF;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x6240;&#x6709;&#x79D1;&#x76EE;&#x5E73;&#x5747;&#x5206;&#xFF0C;2&#x7EF4;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th>test</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <th>&#x4E00;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x674E;&#x56DB;</th>
      <th>&#x4E00;</th>
      <td>99</td>
      <td>98</td>
      <td>84</td>
    </tr>
    <tr>
      <th>&#x4E8C;</th>
      <td>46</td>
      <td>56</td>
      <td>95</td>
    </tr>
    <tr>
      <th rowspan="3" valign="top">&#x738B;&#x4E94;</th>
      <th>&#x4E00;</th>
      <td>99</td>
      <td>96</td>
      <td>47</td>
    </tr>
    <tr>
      <th>&#x4E09;</th>
      <td>89</td>
      <td>82</td>
      <td>64</td>
    </tr>
    <tr>
      <th>&#x4E8C;</th>
      <td>54</td>
      <td>76</td>
      <td>71</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <th>&#x4E00;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby([<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;test&apos;</span>])[<span class="hljs-string">&apos;math&apos;</span>].mean()    <span class="hljs-comment"># &#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#xFF0C;&#x6BCF;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x6570;&#x5B66;&#x79D1;&#x76EE;&#x5E73;&#x5747;&#x5206;&#xFF0C;2&#x7EF4;</span>
</code></pre>
<pre><code>name  test
&#x5F20;&#x4E09;    &#x4E00;       89
&#x674E;&#x56DB;    &#x4E00;       98
      &#x4E8C;       56
&#x738B;&#x4E94;    &#x4E00;       96
      &#x4E09;       82
      &#x4E8C;       76
&#x8D75;&#x516D;    &#x4E00;       46
Name: math, dtype: int32
</code></pre><p>&#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#x5728;&#x6BCF;&#x6B21;&#x8003;&#x8BD5;&#x6B21;&#x6570;&#x4E2D;&#x7684;&#x6570;&#x5B66;&#x5E73;&#x5747;&#x5206;</p>
<p>&#x6700;&#x7EC8;&#x7248;&#xFF0C;&#x4E8C;&#x7EF4;&#x8868;&#x683C;&#x5F62;&#x5F0F;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8F6C;&#x4E3A;3&#x7EF4;</span>
df.groupby([<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;test&apos;</span>])[<span class="hljs-string">&apos;math&apos;</span>].mean().unstack().fillna(<span class="hljs-number">0</span>)  <span class="hljs-comment"># &#x5C06;&#x5185;&#x5C42;&#x7D22;&#x5F15;&#x65CB;&#x8F6C;&#x4E3A;&#x5217;&#x7D22;&#x5F15;&#xFF0C;&#x7F3A;&#x5931;&#x503C;&#x586B;&#x5145;&#x4E3A;0</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>test</th>
      <th>&#x4E00;</th>
      <th>&#x4E09;</th>
      <th>&#x4E8C;</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>89.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>98.0</td>
      <td>0.0</td>
      <td>56.0</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>96.0</td>
      <td>82.0</td>
      <td>76.0</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>46.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
  </tbody>
</table>
</div>



<hr>
<h1 id="&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x65B9;&#x5F0F;">&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x65B9;&#x5F0F;</h1>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x65B9;&#x5F0F;&#xFF1A;aggregate()&#xFF0C;&#x6216;agg()</p>
<p>&#x4E4B;&#x524D;&#x7684;&#x805A;&#x5408;&#x65B9;&#x5F0F;&#xFF0C;&#x6240;&#x6709;&#x5217;&#x53EA;&#x80FD;&#x5E94;&#x7528;&#x4E00;&#x4E2A;&#x76F8;&#x540C;&#x7684;&#x805A;&#x5408;&#x51FD;&#x6570;</p>
<h3 id="agg&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x65B9;&#x5F0F;&#x7684;&#x4F18;&#x52BF;&#xFF1A;">agg()&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x65B9;&#x5F0F;&#x7684;&#x4F18;&#x52BF;&#xFF1A;</h3>
<ul>
<li>&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x5217;&#x8868;<ul>
<li>&#x5BF9;&#x6570;&#x636E;&#x6BCF;&#x5217;&#x5E94;&#x7528;&#x591A;&#x4E2A;&#x76F8;&#x540C;&#x7684;&#x805A;&#x5408;&#x51FD;&#x6570;</li>
</ul>
</li>
<li>&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x5178;<ul>
<li>&#x5BF9;&#x6570;&#x636E;&#x7684;&#x6BCF;&#x5217;&#x5E94;&#x7528;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x805A;&#x5408;&#x51FD;&#x6570;</li>
</ul>
</li>
<li><p>&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;</p>
<ul>
<li>&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x590D;&#x6742;&#x7684;&#x64CD;&#x4F5C;
&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#xFF1A;</li>
</ul>
</li>
<li><p>&#x6BCF;&#x4E2A;&#x5217;&#x5E94;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x805A;&#x5408;&#x65B9;&#x5F0F;</p>
</li>
<li>&#x4E00;&#x4E2A;&#x5217;&#x5E94;&#x7528;&#x591A;&#x4E2A;&#x805A;&#x5408;&#x65B9;&#x5F0F;</li>
</ul>
<pre><code class="lang-python">df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x666E;&#x901A;&#x5206;&#x7EC4;&#x805A;&#x5408;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>254</td>
      <td>182</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x805A;&#x5408;&#x7684;&#x5199;&#x6CD5;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg(<span class="hljs-string">&apos;sum&apos;</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>154</td>
      <td>179</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>254</td>
      <td>182</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>



<h4 id="&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x5217;&#x8868;">&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x5217;&#x8868;</h4>
<p>&#x7ED9;&#x6BCF;&#x4E00;&#x5217;&#x540C;&#x65F6;&#x5E94;&#x7528;&#x5BF9;&#x4E2A;&#x805A;&#x5408;&#x51FD;&#x6570;</p>
<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg([sum,<span class="hljs-string">&apos;mean&apos;</span>,np.min])  <span class="hljs-comment">#&#x5217;&#x8868;&#x53C2;&#x6570;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x79CD;&#x4E0D;&#x540C;&#x5199;&#x6CD5;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="3" halign="left">chinese</th>
      <th colspan="3" halign="left">math</th>
      <th colspan="3" halign="left">english</th>
    </tr>
    <tr>
      <th></th>
      <th>sum</th>
      <th>mean</th>
      <th>amin</th>
      <th>sum</th>
      <th>mean</th>
      <th>amin</th>
      <th>sum</th>
      <th>mean</th>
      <th>amin</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>47.000000</td>
      <td>47</td>
      <td>89</td>
      <td>89.000000</td>
      <td>89</td>
      <td>82</td>
      <td>82.000000</td>
      <td>82</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>72.500000</td>
      <td>46</td>
      <td>154</td>
      <td>77.000000</td>
      <td>56</td>
      <td>179</td>
      <td>89.500000</td>
      <td>84</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>80.666667</td>
      <td>54</td>
      <td>254</td>
      <td>84.666667</td>
      <td>76</td>
      <td>182</td>
      <td>60.666667</td>
      <td>47</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>82.000000</td>
      <td>82</td>
      <td>46</td>
      <td>46.000000</td>
      <td>46</td>
      <td>85</td>
      <td>85.000000</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x5C06;&#x805A;&#x5408;&#x5217;&#x7D22;&#x5F15;&#x6539;&#x4E3A;&#x81EA;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#xFF0C;&#x5143;&#x7EC4;&#x5B9E;&#x73B0;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>)[<span class="hljs-string">&apos;chinese&apos;</span>,<span class="hljs-string">&apos;math&apos;</span>].agg([(<span class="hljs-string">&apos;&#x6C42;&#x548C;&apos;</span>,sum),(<span class="hljs-string">&apos;&#x5E73;&#x5747;&#x503C;&apos;</span>,<span class="hljs-string">&apos;mean&apos;</span>),(<span class="hljs-string">&apos;&#x6700;&#x5C0F;&#x503C;&apos;</span>,<span class="hljs-string">&apos;min&apos;</span>)])
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="3" halign="left">chinese</th>
      <th colspan="3" halign="left">math</th>
    </tr>
    <tr>
      <th></th>
      <th>&#x6C42;&#x548C;</th>
      <th>&#x5E73;&#x5747;&#x503C;</th>
      <th>&#x6700;&#x5C0F;&#x503C;</th>
      <th>&#x6C42;&#x548C;</th>
      <th>&#x5E73;&#x5747;&#x503C;</th>
      <th>&#x6700;&#x5C0F;&#x503C;</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>47.000000</td>
      <td>47</td>
      <td>89</td>
      <td>89.000000</td>
      <td>89</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>72.500000</td>
      <td>46</td>
      <td>154</td>
      <td>77.000000</td>
      <td>56</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>80.666667</td>
      <td>54</td>
      <td>254</td>
      <td>84.666667</td>
      <td>76</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>82.000000</td>
      <td>82</td>
      <td>46</td>
      <td>46.000000</td>
      <td>46</td>
    </tr>
  </tbody>
</table>
</div>



<h3 id="&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x5178;">&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x5178;</h3>
<p>&#x6BCF;&#x5217;&#x5E94;&#x7528;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x805A;&#x5408;&#x51FD;&#x6570;&#xFF0C;&#x6216;&#x8005;&#x6BCF;&#x5217;&#x5E94;&#x7528;&#x591A;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x805A;&#x5408;&#x51FD;&#x6570;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8BED;&#x6587;&#x5217;&#x805A;&#x5408;&#x51FD;&#x6570;&#xFF0C;&#x6C42;&#x548C;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg({<span class="hljs-string">&apos;chinese&apos;</span>:sum})
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x8BED;&#x6587;&#x5217;&#x805A;&#x5408;&#x51FD;&#x6570;&#xFF1A;&#x6C42;&#x548C;&#xFF0C;&#x5E73;&#x5747;&#x503C;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg({<span class="hljs-string">&apos;chinese&apos;</span>:[sum,<span class="hljs-string">&apos;mean&apos;</span>]})
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="2" halign="left">chinese</th>
    </tr>
    <tr>
      <th></th>
      <th>sum</th>
      <th>mean</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>47.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>72.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>80.666667</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>82.000000</td>
    </tr>
  </tbody>
</table>
</div>



<p>&#x9009;&#x4E2D;&#x7684;&#x591A;&#x4E2A;&#x5217;&#xFF0C;&#x6BCF;&#x5217;&#x90FD;&#x5E94;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x591A;&#x4E2A;&#x805A;&#x5408;&#x51FD;&#x6570;</p>
<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg({<span class="hljs-string">&apos;chinese&apos;</span>:[sum,<span class="hljs-string">&apos;mean&apos;</span>],<span class="hljs-string">&apos;math&apos;</span>: [sum,<span class="hljs-string">&apos;mean&apos;</span>, max]})
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="2" halign="left">chinese</th>
      <th colspan="3" halign="left">math</th>
    </tr>
    <tr>
      <th></th>
      <th>sum</th>
      <th>mean</th>
      <th>sum</th>
      <th>mean</th>
      <th>max</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47</td>
      <td>47.000000</td>
      <td>89</td>
      <td>89.000000</td>
      <td>89</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>145</td>
      <td>72.500000</td>
      <td>154</td>
      <td>77.000000</td>
      <td>98</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>242</td>
      <td>80.666667</td>
      <td>254</td>
      <td>84.666667</td>
      <td>96</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82</td>
      <td>82.000000</td>
      <td>46</td>
      <td>46.000000</td>
      <td>46</td>
    </tr>
  </tbody>
</table>
</div>



<h3 id="&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;">&#x805A;&#x5408;&#x53C2;&#x6570;&#x662F;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;</h3>
<p>&#x7528;&#x4E8E;&#x4E00;&#x4E9B;&#x8F83;&#x4E3A;&#x590D;&#x6742;&#x7684;&#x805A;&#x5408;&#x5DE5;&#x4F5C;</p>
<ul>
<li>&#x81EA;&#x5B9A;&#x4E49;&#x805A;&#x5408;&#x51FD;&#x6570;&#x8981;&#x6BD4;&#x7CFB;&#x7EDF;&#x81EA;&#x5E26;&#x7684;&#x3001;&#x7ECF;&#x8FC7;&#x4F18;&#x5316;&#x7684;&#x51FD;&#x6570;&#x6162;&#x5F97;&#x591A;&#x3002;</li>
<li>&#x56E0;&#x4E3A;&#x5728;&#x6784;&#x9020;&#x4E2D;&#x95F4;&#x5206;&#x7EC4;&#x6570;&#x636E;&#x5757;&#x65F6;&#x5B58;&#x5728;&#x975E;&#x5E38;&#x5927;&#x7684;&#x5F00;&#x9500;&#xFF08;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3001;&#x6570;&#x636E;&#x91CD;&#x6392;&#x7B49;&#xFF09;</li>
</ul>
<pre><code class="lang-python">df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">aaa</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">return</span> x.max() - x.min()

df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg(aaa)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>53</td>
      <td>42</td>
      <td>11</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>45</td>
      <td>20</td>
      <td>24</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>



<hr>
<h3 id="&#x5176;&#x4ED6;&#x5206;&#x7EC4;&#x8FD0;&#x7B97;">&#x5176;&#x4ED6;&#x5206;&#x7EC4;&#x8FD0;&#x7B97;</h3>
<p>&#x8FD0;&#x7528;groupby &#x51FD;&#x6570;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x8FD8;&#x6709;&#x5F88;&#x591A;&#xFF0C;&#x5E76;&#x4E0D;&#x5C40;&#x9650;&#x4E8E;&#x805A;&#x5408;&#x6C47;&#x603B;</p>
<pre><code class="lang-python">df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>



<p>&#x8FC7;&#x6EE4;&#x6570;&#x636E;</p>
<p>&#x6817;&#x5B50;&#xFF1A; &#x8F93;&#x51FA;&#x6240;&#x6709;&#x6570;&#x5B66;&#x8003;&#x8BD5;&#x5E73;&#x5747;&#x5206;&#x53CA;&#x683C;&#x7684;&#x5B66;&#x751F;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bbb</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">return</span> x[<span class="hljs-string">&apos;math&apos;</span>].mean() &gt;= <span class="hljs-number">60</span>

df.groupby(<span class="hljs-string">&apos;name&apos;</span>).agg(bbb)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>True</td>
      <td>True</td>
      <td>True</td>
      <td>True</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>True</td>
      <td>True</td>
      <td>True</td>
      <td>True</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>True</td>
      <td>True</td>
      <td>True</td>
      <td>True</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>False</td>
      <td>False</td>
      <td>False</td>
      <td>False</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">e = df.groupby(<span class="hljs-string">&apos;name&apos;</span>).filter(bbb)
e
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).filter(bbb).groupby(<span class="hljs-string">&apos;name&apos;</span>).mean()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>72.500000</td>
      <td>77.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>80.666667</td>
      <td>84.666667</td>
      <td>60.666667</td>
    </tr>
  </tbody>
</table>
</div>



<p>&#x4F7F;&#x7528;transform &#x51FD;&#x6570;&#x5BF9;&#x6240;&#x6709;&#x7684;&#x6570;&#x636E;&#x5143;&#x7D20;&#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x8BA1;&#x7B97;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fff</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">return</span> x + <span class="hljs-number">10</span>

df.groupby(<span class="hljs-string">&apos;name&apos;</span>).transform(fff)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>57</td>
      <td>99</td>
      <td>92</td>
    </tr>
    <tr>
      <th>1</th>
      <td>109</td>
      <td>108</td>
      <td>94</td>
    </tr>
    <tr>
      <th>2</th>
      <td>109</td>
      <td>106</td>
      <td>57</td>
    </tr>
    <tr>
      <th>3</th>
      <td>56</td>
      <td>66</td>
      <td>105</td>
    </tr>
    <tr>
      <th>4</th>
      <td>64</td>
      <td>86</td>
      <td>81</td>
    </tr>
    <tr>
      <th>5</th>
      <td>99</td>
      <td>92</td>
      <td>74</td>
    </tr>
    <tr>
      <th>6</th>
      <td>92</td>
      <td>56</td>
      <td>95</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x4F7F;&#x7528;&#x5411;&#x91CF;&#x5316;&#x8FD0;&#x7B97;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;</span>
df.loc[:,[<span class="hljs-string">&apos;chinese&apos;</span>,<span class="hljs-string">&apos;math&apos;</span>,<span class="hljs-string">&apos;english&apos;</span>]] + <span class="hljs-number">10</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>57</td>
      <td>99</td>
      <td>92</td>
    </tr>
    <tr>
      <th>1</th>
      <td>109</td>
      <td>108</td>
      <td>94</td>
    </tr>
    <tr>
      <th>2</th>
      <td>109</td>
      <td>106</td>
      <td>57</td>
    </tr>
    <tr>
      <th>3</th>
      <td>56</td>
      <td>66</td>
      <td>105</td>
    </tr>
    <tr>
      <th>4</th>
      <td>64</td>
      <td>86</td>
      <td>81</td>
    </tr>
    <tr>
      <th>5</th>
      <td>99</td>
      <td>92</td>
      <td>74</td>
    </tr>
    <tr>
      <th>6</th>
      <td>92</td>
      <td>56</td>
      <td>95</td>
    </tr>
  </tbody>
</table>
</div>



<p>apply&#x662F;&#x66F4;&#x5E95;&#x5C42;&#x7684;&#x51FD;&#x6570;</p>
<pre><code class="lang-python"><span class="hljs-comment">#&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fff</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">return</span> x + <span class="hljs-number">10</span>
df.loc[:,[<span class="hljs-string">&apos;chinese&apos;</span>,<span class="hljs-string">&apos;math&apos;</span>,<span class="hljs-string">&apos;english&apos;</span>]].apply(fff)   <span class="hljs-comment">#&#x6548;&#x679C;&#x540C;&#x4E0A;&#xFF0C;&#x5E95;&#x5C42;&#x5199;&#x6CD5;&#xFF0C;&#x8F83;&#x4E3A;&#x7E41;&#x7410;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>57</td>
      <td>99</td>
      <td>92</td>
    </tr>
    <tr>
      <th>1</th>
      <td>109</td>
      <td>108</td>
      <td>94</td>
    </tr>
    <tr>
      <th>2</th>
      <td>109</td>
      <td>106</td>
      <td>57</td>
    </tr>
    <tr>
      <th>3</th>
      <td>56</td>
      <td>66</td>
      <td>105</td>
    </tr>
    <tr>
      <th>4</th>
      <td>64</td>
      <td>86</td>
      <td>81</td>
    </tr>
    <tr>
      <th>5</th>
      <td>99</td>
      <td>92</td>
      <td>74</td>
    </tr>
    <tr>
      <th>6</th>
      <td>92</td>
      <td>56</td>
      <td>95</td>
    </tr>
  </tbody>
</table>
</div>



<h2 id="&#x4F8B;&#x5B50;-&#xFF1A;-&#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#xFF0C;&#x6BCF;&#x79D1;&#x6210;&#x7EE9;&#x7684;&#x5E73;&#x5747;&#x503C;">&#x4F8B;&#x5B50; &#xFF1A; &#x6BCF;&#x4E2A;&#x5B66;&#x751F;&#xFF0C;&#x6BCF;&#x79D1;&#x6210;&#x7EE9;&#x7684;&#x5E73;&#x5747;&#x503C;</h2>
<pre><code class="lang-python"><span class="hljs-comment">#&#x6B63;&#x5E38;&#x5206;&#x7EC4;&#x805A;&#x5408;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).mean()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>72.500000</td>
      <td>77.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>80.666667</td>
      <td>84.666667</td>
      <td>60.666667</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#apply &#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#xFF0C;&#x5E95;&#x5C42;&#x5199;&#x6CD5;&#xFF0C;&#x805A;&#x5408;&#x7684;&#x539F;&#x7406;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ggg</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">return</span> x.mean()
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(ggg)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>72.500000</td>
      <td>77.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>80.666667</td>
      <td>84.666667</td>
      <td>60.666667</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
  </tbody>
</table>
</div>



<h3 id="&#x7EC3;&#x4E60;&#xFF1A;&#x5206;&#x7EC4;&#x540E;&#x8FDB;&#x884C;&#x590D;&#x6742;&#x8BA1;&#x7B97;&#xFF0C;&#x4F7F;&#x7528;apply&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;">&#x7EC3;&#x4E60;&#xFF1A;&#x5206;&#x7EC4;&#x540E;&#x8FDB;&#x884C;&#x590D;&#x6742;&#x8BA1;&#x7B97;&#xFF0C;&#x4F7F;&#x7528;apply&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;</h3>
<ul>
<li>&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x5168;&#x90E8;&#x52A0;10</li>
<li>&#x6570;&#x5B66;&#x6210;&#x7EE9;&#x5168;&#x90E8;&#x51CF;10</li>
<li>&#x6C42;&#x6BCF;&#x4F4D;&#x540C;&#x5B66;&#xFF0C;&#x6BCF;&#x79D1;&#x6210;&#x7EE9;&#xFF0C;&#x5E73;&#x5747;&#x5206;</li>
</ul>
<pre><code class="lang-python">df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x666E;&#x901A;&#x5199;&#x6CD5;</span>
df3 = df.copy()
df3
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x8BED;&#x6587;&#x52A0;10 &#x5206;</span>
df3[<span class="hljs-string">&apos;chinese&apos;</span>] = df3[<span class="hljs-string">&apos;chinese&apos;</span>] + <span class="hljs-number">10</span>
df3
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>57</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>109</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>109</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>56</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>64</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>92</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x6570;&#x5B66;&#x51CF;10 &#x5206;</span>
df3[<span class="hljs-string">&apos;math&apos;</span>] -= <span class="hljs-number">10</span>
df3
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>57</td>
      <td>79</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>109</td>
      <td>88</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>109</td>
      <td>86</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>56</td>
      <td>46</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>64</td>
      <td>66</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>72</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>92</td>
      <td>36</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x5206;&#x7EC4;&#x805A;&#x5408;</span>
df3.groupby(<span class="hljs-string">&apos;name&apos;</span>).mean()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>57.000000</td>
      <td>79.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>82.500000</td>
      <td>67.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>90.666667</td>
      <td>74.666667</td>
      <td>60.666667</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>92.000000</td>
      <td>36.000000</td>
      <td>85.000000</td>
    </tr>
  </tbody>
</table>
</div>




<hr>
<hr>
<pre><code class="lang-python">
<span class="hljs-comment"># apply &#x5B9E;&#x73B0;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hh</span><span class="hljs-params">(x)</span>:</span>
    x[<span class="hljs-string">&apos;chinese&apos;</span>] += <span class="hljs-number">10</span>
    x[<span class="hljs-string">&apos;math&apos;</span>] -= <span class="hljs-number">10</span>
    <span class="hljs-keyword">return</span> x.mean()
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(hh)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>67.000000</td>
      <td>69.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>82.500000</td>
      <td>67.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>90.666667</td>
      <td>74.666667</td>
      <td>60.666667</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>92.000000</td>
      <td>36.000000</td>
      <td>85.000000</td>
    </tr>
  </tbody>
</table>
</div>



<hr>
<h3 id="&#x4F8B;&#x5B50;&#xFF1A;&#x5C06;&#x5B66;&#x751F;&#x67D0;&#x79D1;&#x6210;&#x7EE9;&#x6309;&#x7531;&#x4F4E;&#x5230;&#x9AD8;&#x6392;&#x5E8F;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x9700;&#x8981;&#x7684;&#x4E2A;&#x6570;">&#x4F8B;&#x5B50;&#xFF1A;&#x5C06;&#x5B66;&#x751F;&#x67D0;&#x79D1;&#x6210;&#x7EE9;&#x6309;&#x7531;&#x4F4E;&#x5230;&#x9AD8;&#x6392;&#x5E8F;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x9700;&#x8981;&#x7684;&#x4E2A;&#x6570;</h3>
<ul>
<li>&#x8FD4;&#x56DE;&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;&#x524D;&#x4E09;&#x6761;&#x6570;&#x636E;</li>
<li>&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x540C;&#x5B66;&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;1&#x6B21;&#x8003;&#x8BD5;&#x6210;&#x7EE9;</li>
<li>&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x540C;&#x5B66;&#x6570;&#x5B66;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;2&#x6B21;&#x8003;&#x8BD5;&#x6210;&#x7EE9;</li>
</ul>
<pre><code class="lang-python">df
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>



<p><strong>&#x6392;&#x5E8F;&#x77E5;&#x8BC6;</strong></p>
<p>a.sort_values()  <strong>&#x6B63;&#x5E8F;</strong></p>
<p>a.sort_values(ascending=False)   <strong>&#x5012;&#x5E8F;</strong></p>
<pre><code class="lang-python"><span class="hljs-comment">#&#x6392;&#x5E8F;&#x77E5;&#x8BC6;</span>
a = pd.Series([<span class="hljs-number">3</span>,<span class="hljs-number">5</span>,<span class="hljs-number">1</span>,<span class="hljs-number">9</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>])
a
</code></pre>
<pre><code>0    3
1    5
2    1
3    9
4    2
5    4
dtype: int64
</code></pre><pre><code class="lang-python">a.sort_values()  <span class="hljs-comment">#&#x6B63;&#x5E8F;</span>
</code></pre>
<pre><code>2    1
4    2
0    3
5    4
1    5
3    9
dtype: int64
</code></pre><pre><code class="lang-python">a.sort_values(ascending=<span class="hljs-keyword">False</span>)   <span class="hljs-comment">#&#x5012;&#x5E8F;</span>
</code></pre>
<pre><code>3    9
1    5
5    4
0    3
4    2
2    1
dtype: int64
</code></pre><p>DataFrame &#x6309;&#x5217;&#x6392;&#x5E8F;</p>
<pre><code class="lang-python">df.sort_values(by=<span class="hljs-string">&apos;chinese&apos;</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.sort_values(by=<span class="hljs-string">&apos;chinese&apos;</span>,ascending = <span class="hljs-keyword">False</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x8FD4;&#x56DE;&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;&#x524D;&#x4E09;&#x6761;&#x6570;&#x636E;</span>
df.sort_values(by=<span class="hljs-string">&apos;chinese&apos;</span>)[:<span class="hljs-number">3</span>]
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x8FD4;&#x56DE;&#x6240;&#x6709;&#x540C;&#x5B66;&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;1&#x6B21;&#x8003;&#x8BD5;&#x6210;&#x7EE9;</span>
df.sort_values(by=<span class="hljs-string">&apos;chinese&apos;</span>)[:<span class="hljs-number">1</span>]
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x8FD4;&#x56DE;&#x6240;&#x6709;&#x540C;&#x5B66;&#x6570;&#x5B66;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;2&#x6B21;&#x8003;&#x8BD5;&#x6210;&#x7EE9;</span>
df.sort_values(by=<span class="hljs-string">&apos;math&apos;</span>)[:<span class="hljs-number">2</span>]
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x4E0A;&#x9762;&#x529F;&#x80FD;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">top</span><span class="hljs-params">(x, p=<span class="hljs-string">&apos;chinese&apos;</span>, n=<span class="hljs-number">3</span>)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x5B9E;&#x73B0;DataFram&#x5BF9;&#x8C61;&#x6392;&#x5E8F;&#x8F93;&#x51FA;&#x529F;&#x80FD;.

    x&#xFF1A;&#x4F20;&#x5165;&#x7684;DataFrame&#x5BF9;&#x8C61;
    n&#xFF1A;&#x83B7;&#x53D6;&#x524D;&#x51E0;&#x4E2A;&#x503C;
    p&#xFF1A;&#x6309;df&#x5BF9;&#x8C61;&#x7684;&#x54EA;&#x4E00;&#x5217;&#x6392;&#x5E8F;
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">return</span> x.sort_values(by=p)[:n]
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8FD4;&#x56DE;&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;&#x524D;&#x4E09;&#x6761;&#x6570;&#x636E;</span>
top(df)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x8FD4;&#x56DE;&#x6240;&#x6709;&#x540C;&#x5B66;&#x8BED;&#x6587;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;1&#x6B21;&#x8003;&#x8BD5;&#x6210;&#x7EE9;</span>
top(df, n=<span class="hljs-number">1</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x8FD4;&#x56DE;&#x6240;&#x6709;&#x540C;&#x5B66;&#x6570;&#x5B66;&#x6210;&#x7EE9;&#x6700;&#x4F4E;&#x7684;2&#x6B21;&#x8003;&#x8BD5;&#x6210;&#x7EE9;</span>
top(df, p=<span class="hljs-string">&apos;math&apos;</span>, n=<span class="hljs-number">2</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
  </tbody>
</table>
</div>



<p>&#x4F7F;&#x7528;apply&#x65B9;&#x5F0F;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x5B9E;&#x73B0;</p>
<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(top)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x674E;&#x56DB;</th>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="3" valign="top">&#x738B;&#x4E94;</th>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x738B;&#x4E94;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(top, p=<span class="hljs-string">&apos;math&apos;</span>, n=<span class="hljs-number">2</span>)  <span class="hljs-comment"># apply&#x901A;&#x8FC7;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4F20;&#x53C2;&#x6570;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x674E;&#x56DB;</th>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x738B;&#x4E94;</th>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>



<h3 id="&#x7981;&#x6B62;&#x5206;&#x7EC4;&#x952E;">&#x7981;&#x6B62;&#x5206;&#x7EC4;&#x952E;</h3>
<p>&#x5206;&#x7EC4;&#x952E;&#x4F1A;&#x8DDF;&#x539F;&#x59CB;&#x5BF9;&#x8C61;&#x7684;&#x7D22;&#x5F15;&#x5171;&#x540C;&#x6784;&#x6210;&#x7ED3;&#x679C;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;</p>
<p>&#x5C06;group_keys=False&#x4F20;&#x5165;groupby&#x5373;&#x53EF;&#x7981;&#x6B62;&#x8BE5;&#x6548;&#x679C;</p>
<p>group_key &#x5220;&#x9664;&#x6700;&#x5916;&#x5C42;&#x7684;&#x884C;&#x7D22;&#x5F15;</p>
<pre><code class="lang-python">df.groupby([<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;test&apos;</span>]).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th>test</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <th>&#x4E00;</th>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x674E;&#x56DB;</th>
      <th>&#x4E00;</th>
      <td>99</td>
      <td>98</td>
      <td>84</td>
    </tr>
    <tr>
      <th>&#x4E8C;</th>
      <td>46</td>
      <td>56</td>
      <td>95</td>
    </tr>
    <tr>
      <th rowspan="3" valign="top">&#x738B;&#x4E94;</th>
      <th>&#x4E00;</th>
      <td>99</td>
      <td>96</td>
      <td>47</td>
    </tr>
    <tr>
      <th>&#x4E09;</th>
      <td>89</td>
      <td>82</td>
      <td>64</td>
    </tr>
    <tr>
      <th>&#x4E8C;</th>
      <td>54</td>
      <td>76</td>
      <td>71</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <th>&#x4E00;</th>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby([<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;test&apos;</span>], as_index=<span class="hljs-keyword">False</span>).sum()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>test</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>&#x4E00;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>&#x4E00;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
    </tr>
    <tr>
      <th>2</th>
      <td>&#x674E;&#x56DB;</td>
      <td>&#x4E8C;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x738B;&#x4E94;</td>
      <td>&#x4E00;</td>
      <td>99</td>
      <td>96</td>
      <td>47</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>&#x4E09;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>&#x4E8C;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>&#x4E00;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment">#&#x5220;&#x9664;&#xFF0C;&#x5220;&#x9664;&#x5206;&#x7EC4;&#x5E26;&#x6765;&#x7684;&#x5916;&#x5C42;&#x7D22;&#x5F15;</span>
df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(top,n=<span class="hljs-number">2</span>,p=<span class="hljs-string">&apos;math&apos;</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x674E;&#x56DB;</th>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">&#x738B;&#x4E94;</th>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>,group_keys = <span class="hljs-keyword">False</span>).apply(top,n=<span class="hljs-number">2</span>,p=<span class="hljs-string">&apos;math&apos;</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>,as_index=<span class="hljs-keyword">False</span>).apply(top,n=<span class="hljs-number">2</span>,p=<span class="hljs-string">&apos;math&apos;</span>)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>name</th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <th>0</th>
      <td>&#x5F20;&#x4E09;</td>
      <td>47</td>
      <td>89</td>
      <td>82</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">1</th>
      <th>3</th>
      <td>&#x674E;&#x56DB;</td>
      <td>46</td>
      <td>56</td>
      <td>95</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>1</th>
      <td>&#x674E;&#x56DB;</td>
      <td>99</td>
      <td>98</td>
      <td>84</td>
      <td>&#x4E00;</td>
    </tr>
    <tr>
      <th rowspan="2" valign="top">2</th>
      <th>4</th>
      <td>&#x738B;&#x4E94;</td>
      <td>54</td>
      <td>76</td>
      <td>71</td>
      <td>&#x4E8C;</td>
    </tr>
    <tr>
      <th>5</th>
      <td>&#x738B;&#x4E94;</td>
      <td>89</td>
      <td>82</td>
      <td>64</td>
      <td>&#x4E09;</td>
    </tr>
    <tr>
      <th>3</th>
      <th>6</th>
      <td>&#x8D75;&#x516D;</td>
      <td>82</td>
      <td>46</td>
      <td>85</td>
      <td>&#x4E00;</td>
    </tr>
  </tbody>
</table>
</div>



<h3 id="groupby&#x8C03;&#x7528;describe&#x65B9;&#x6CD5;">groupby&#x8C03;&#x7528;describe()&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>)[<span class="hljs-string">&apos;chinese&apos;</span>].mean()
</code></pre>
<pre><code>name
&#x5F20;&#x4E09;    47.000000
&#x674E;&#x56DB;    72.500000
&#x738B;&#x4E94;    80.666667
&#x8D75;&#x516D;    82.000000
Name: chinese, dtype: float64
</code></pre><pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>)[<span class="hljs-string">&apos;chinese&apos;</span>].describe()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>count</th>
      <th>mean</th>
      <th>std</th>
      <th>min</th>
      <th>25%</th>
      <th>50%</th>
      <th>75%</th>
      <th>max</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>1.0</td>
      <td>47.000000</td>
      <td>NaN</td>
      <td>47.0</td>
      <td>47.00</td>
      <td>47.0</td>
      <td>47.00</td>
      <td>47.0</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>2.0</td>
      <td>72.500000</td>
      <td>37.476659</td>
      <td>46.0</td>
      <td>59.25</td>
      <td>72.5</td>
      <td>85.75</td>
      <td>99.0</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>3.0</td>
      <td>80.666667</td>
      <td>23.629078</td>
      <td>54.0</td>
      <td>71.50</td>
      <td>89.0</td>
      <td>94.00</td>
      <td>99.0</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>1.0</td>
      <td>82.000000</td>
      <td>NaN</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>)[<span class="hljs-string">&apos;chinese&apos;</span>].describe().stack()  <span class="hljs-comment"># &#x5217;&#x7D22;&#x5F15;&#x8F6C;&#x4E3A;&#x884C;&#x7D22;&#x5F15;</span>
</code></pre>
<pre><code>name       
&#x5F20;&#x4E09;    count     1.000000
      mean     47.000000
      min      47.000000
      25%      47.000000
      50%      47.000000
      75%      47.000000
      max      47.000000
&#x674E;&#x56DB;    count     2.000000
      mean     72.500000
      std      37.476659
      min      46.000000
      25%      59.250000
      50%      72.500000
      75%      85.750000
      max      99.000000
&#x738B;&#x4E94;    count     3.000000
      mean     80.666667
      std      23.629078
      min      54.000000
      25%      71.500000
      50%      89.000000
      75%      94.000000
      max      99.000000
&#x8D75;&#x516D;    count     1.000000
      mean     82.000000
      min      82.000000
      25%      82.000000
      50%      82.000000
      75%      82.000000
      max      82.000000
dtype: float64
</code></pre><pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>)[<span class="hljs-string">&apos;chinese&apos;</span>].describe().unstack()  <span class="hljs-comment"># &#x884C;&#x7D22;&#x5F15;&#x8F6C;&#x4E3A;&#x5217;&#x7D22;&#x5F15;</span>
</code></pre>
<pre><code>       name
count  &#x5F20;&#x4E09;       1.000000
       &#x674E;&#x56DB;       2.000000
       &#x738B;&#x4E94;       3.000000
       &#x8D75;&#x516D;       1.000000
mean   &#x5F20;&#x4E09;      47.000000
       &#x674E;&#x56DB;      72.500000
       &#x738B;&#x4E94;      80.666667
       &#x8D75;&#x516D;      82.000000
std    &#x5F20;&#x4E09;            NaN
       &#x674E;&#x56DB;      37.476659
       &#x738B;&#x4E94;      23.629078
       &#x8D75;&#x516D;            NaN
min    &#x5F20;&#x4E09;      47.000000
       &#x674E;&#x56DB;      46.000000
       &#x738B;&#x4E94;      54.000000
       &#x8D75;&#x516D;      82.000000
25%    &#x5F20;&#x4E09;      47.000000
       &#x674E;&#x56DB;      59.250000
       &#x738B;&#x4E94;      71.500000
       &#x8D75;&#x516D;      82.000000
50%    &#x5F20;&#x4E09;      47.000000
       &#x674E;&#x56DB;      72.500000
       &#x738B;&#x4E94;      89.000000
       &#x8D75;&#x516D;      82.000000
75%    &#x5F20;&#x4E09;      47.000000
       &#x674E;&#x56DB;      85.750000
       &#x738B;&#x4E94;      94.000000
       &#x8D75;&#x516D;      82.000000
max    &#x5F20;&#x4E09;      47.000000
       &#x674E;&#x56DB;      99.000000
       &#x738B;&#x4E94;      99.000000
       &#x8D75;&#x516D;      82.000000
dtype: float64
</code></pre><p>DataFrame&#x5206;&#x7EC4;&#x540E;&#x80FD;&#x5E94;&#x7528;describe&#x7684;&#x539F;&#x56E0;:</p>
<p>&#x8F6C;&#x5316;&#x540E;&#x7684;&#x8868;&#x683C;&#x5F0F;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#xFF0C;&#x53EF;&#x4EE5;&#x6A21;&#x62DF;&#x4E09;&#x7EF4;&#x6570;&#x636E;</p>
<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).describe()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="8" halign="left">chinese</th>
      <th colspan="5" halign="left">english</th>
      <th colspan="8" halign="left">math</th>
    </tr>
    <tr>
      <th></th>
      <th>count</th>
      <th>mean</th>
      <th>std</th>
      <th>min</th>
      <th>25%</th>
      <th>50%</th>
      <th>75%</th>
      <th>max</th>
      <th>count</th>
      <th>mean</th>
      <th>...</th>
      <th>75%</th>
      <th>max</th>
      <th>count</th>
      <th>mean</th>
      <th>std</th>
      <th>min</th>
      <th>25%</th>
      <th>50%</th>
      <th>75%</th>
      <th>max</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>1.0</td>
      <td>47.000000</td>
      <td>NaN</td>
      <td>47.0</td>
      <td>47.00</td>
      <td>47.0</td>
      <td>47.00</td>
      <td>47.0</td>
      <td>1.0</td>
      <td>82.000000</td>
      <td>...</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>1.0</td>
      <td>89.000000</td>
      <td>NaN</td>
      <td>89.0</td>
      <td>89.0</td>
      <td>89.0</td>
      <td>89.0</td>
      <td>89.0</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>2.0</td>
      <td>72.500000</td>
      <td>37.476659</td>
      <td>46.0</td>
      <td>59.25</td>
      <td>72.5</td>
      <td>85.75</td>
      <td>99.0</td>
      <td>2.0</td>
      <td>89.500000</td>
      <td>...</td>
      <td>92.25</td>
      <td>95.0</td>
      <td>2.0</td>
      <td>77.000000</td>
      <td>29.698485</td>
      <td>56.0</td>
      <td>66.5</td>
      <td>77.0</td>
      <td>87.5</td>
      <td>98.0</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>3.0</td>
      <td>80.666667</td>
      <td>23.629078</td>
      <td>54.0</td>
      <td>71.50</td>
      <td>89.0</td>
      <td>94.00</td>
      <td>99.0</td>
      <td>3.0</td>
      <td>60.666667</td>
      <td>...</td>
      <td>67.50</td>
      <td>71.0</td>
      <td>3.0</td>
      <td>84.666667</td>
      <td>10.263203</td>
      <td>76.0</td>
      <td>79.0</td>
      <td>82.0</td>
      <td>89.0</td>
      <td>96.0</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>1.0</td>
      <td>82.000000</td>
      <td>NaN</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>1.0</td>
      <td>85.000000</td>
      <td>...</td>
      <td>85.00</td>
      <td>85.0</td>
      <td>1.0</td>
      <td>46.000000</td>
      <td>NaN</td>
      <td>46.0</td>
      <td>46.0</td>
      <td>46.0</td>
      <td>46.0</td>
      <td>46.0</td>
    </tr>
  </tbody>
</table>
<p>4 rows &#xD7; 24 columns</p>
</div>



<p>apply&#x662F;&#x805A;&#x5408;&#x64CD;&#x4F5C;&#x7684;&#x5E95;&#x5C42;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-python">xxx = <span class="hljs-keyword">lambda</span> x:x.describe()

df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(xxx)
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>chinese</th>
      <th>math</th>
      <th>english</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="8" valign="top">&#x5F20;&#x4E09;</th>
      <th>count</th>
      <td>1.000000</td>
      <td>1.000000</td>
      <td>1.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>std</th>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>min</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>47.000000</td>
      <td>89.000000</td>
      <td>82.000000</td>
    </tr>
    <tr>
      <th rowspan="8" valign="top">&#x674E;&#x56DB;</th>
      <th>count</th>
      <td>2.000000</td>
      <td>2.000000</td>
      <td>2.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>72.500000</td>
      <td>77.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>std</th>
      <td>37.476659</td>
      <td>29.698485</td>
      <td>7.778175</td>
    </tr>
    <tr>
      <th>min</th>
      <td>46.000000</td>
      <td>56.000000</td>
      <td>84.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>59.250000</td>
      <td>66.500000</td>
      <td>86.750000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>72.500000</td>
      <td>77.000000</td>
      <td>89.500000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>85.750000</td>
      <td>87.500000</td>
      <td>92.250000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>99.000000</td>
      <td>98.000000</td>
      <td>95.000000</td>
    </tr>
    <tr>
      <th rowspan="8" valign="top">&#x738B;&#x4E94;</th>
      <th>count</th>
      <td>3.000000</td>
      <td>3.000000</td>
      <td>3.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>80.666667</td>
      <td>84.666667</td>
      <td>60.666667</td>
    </tr>
    <tr>
      <th>std</th>
      <td>23.629078</td>
      <td>10.263203</td>
      <td>12.342339</td>
    </tr>
    <tr>
      <th>min</th>
      <td>54.000000</td>
      <td>76.000000</td>
      <td>47.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>71.500000</td>
      <td>79.000000</td>
      <td>55.500000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>89.000000</td>
      <td>82.000000</td>
      <td>64.000000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>94.000000</td>
      <td>89.000000</td>
      <td>67.500000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>99.000000</td>
      <td>96.000000</td>
      <td>71.000000</td>
    </tr>
    <tr>
      <th rowspan="8" valign="top">&#x8D75;&#x516D;</th>
      <th>count</th>
      <td>1.000000</td>
      <td>1.000000</td>
      <td>1.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
    <tr>
      <th>std</th>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>min</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>82.000000</td>
      <td>46.000000</td>
      <td>85.000000</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">df.groupby(<span class="hljs-string">&apos;name&apos;</span>).apply(xxx).unstack()  <span class="hljs-comment"># &#x5C06;&#x5185;&#x5C42;&#x884C;&#x7D22;&#x5F15;&#x65CB;&#x8F6C;&#x4E3A;&#x5185;&#x5C42;&#x5217;&#x7D22;&#x5F15;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="8" halign="left">chinese</th>
      <th colspan="5" halign="left">math</th>
      <th colspan="8" halign="left">english</th>
    </tr>
    <tr>
      <th></th>
      <th>count</th>
      <th>mean</th>
      <th>std</th>
      <th>min</th>
      <th>25%</th>
      <th>50%</th>
      <th>75%</th>
      <th>max</th>
      <th>count</th>
      <th>mean</th>
      <th>...</th>
      <th>75%</th>
      <th>max</th>
      <th>count</th>
      <th>mean</th>
      <th>std</th>
      <th>min</th>
      <th>25%</th>
      <th>50%</th>
      <th>75%</th>
      <th>max</th>
    </tr>
    <tr>
      <th>name</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>&#x5F20;&#x4E09;</th>
      <td>1.0</td>
      <td>47.000000</td>
      <td>NaN</td>
      <td>47.0</td>
      <td>47.00</td>
      <td>47.0</td>
      <td>47.00</td>
      <td>47.0</td>
      <td>1.0</td>
      <td>89.000000</td>
      <td>...</td>
      <td>89.0</td>
      <td>89.0</td>
      <td>1.0</td>
      <td>82.000000</td>
      <td>NaN</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
    </tr>
    <tr>
      <th>&#x674E;&#x56DB;</th>
      <td>2.0</td>
      <td>72.500000</td>
      <td>37.476659</td>
      <td>46.0</td>
      <td>59.25</td>
      <td>72.5</td>
      <td>85.75</td>
      <td>99.0</td>
      <td>2.0</td>
      <td>77.000000</td>
      <td>...</td>
      <td>87.5</td>
      <td>98.0</td>
      <td>2.0</td>
      <td>89.500000</td>
      <td>7.778175</td>
      <td>84.0</td>
      <td>86.75</td>
      <td>89.5</td>
      <td>92.25</td>
      <td>95.0</td>
    </tr>
    <tr>
      <th>&#x738B;&#x4E94;</th>
      <td>3.0</td>
      <td>80.666667</td>
      <td>23.629078</td>
      <td>54.0</td>
      <td>71.50</td>
      <td>89.0</td>
      <td>94.00</td>
      <td>99.0</td>
      <td>3.0</td>
      <td>84.666667</td>
      <td>...</td>
      <td>89.0</td>
      <td>96.0</td>
      <td>3.0</td>
      <td>60.666667</td>
      <td>12.342339</td>
      <td>47.0</td>
      <td>55.50</td>
      <td>64.0</td>
      <td>67.50</td>
      <td>71.0</td>
    </tr>
    <tr>
      <th>&#x8D75;&#x516D;</th>
      <td>1.0</td>
      <td>82.000000</td>
      <td>NaN</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>82.00</td>
      <td>82.0</td>
      <td>1.0</td>
      <td>46.000000</td>
      <td>...</td>
      <td>46.0</td>
      <td>46.0</td>
      <td>1.0</td>
      <td>85.000000</td>
      <td>NaN</td>
      <td>85.0</td>
      <td>85.00</td>
      <td>85.0</td>
      <td>85.00</td>
      <td>85.0</td>
    </tr>
  </tbody>
</table>
<p>4 rows &#xD7; 24 columns</p>
</div>



                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../数据分析库的操作/8Pandas分组聚合1.html" class="navigation navigation-prev " aria-label="Previous page: Pandas分组聚合1"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../数据分析库的操作/10Pandas数据规整-清理.html" class="navigation navigation-next " aria-label="Next page: Pandas数据规整-清理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
